package com.wbp.cms.core.user.service;

import java.util.List;

import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.base.Preconditions;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.client.user.domain.RoleDO;
import com.wbp.cms.client.user.query.RoleQuery;
import com.wbp.cms.client.user.service.RoleService;
import com.wbp.cms.core.user.dao.RoleDAO;


/**
 * @Description: 
 * @Department:
 * @author andy.lhc
 * @date 2016年3月20日 上午10:43:43 
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
  
  /**
   * 
   */
  private static final long serialVersionUID = 3485281332838605268L;

  private static final Logger logger = Logger.getLogger(RoleServiceImpl.class) ;
  
  @Resource
  private RoleDAO  roleDAO ;

  @Override
  @Transactional
  public Result<Long> addRole(RoleDO roleDO) {
     
    Preconditions.checkArgument(roleDO !=null, "roleDO不能为空");
    long  i = roleDAO.insert(roleDO);
    if(i <=0){
      logger.error("roleDO:"+roleDO+",新增失败");
      return Result.buildError("保存角色失败") ;
    }
     return Result.buildSuccess(i) ;
  }

   
  @Override
  public Result<Boolean> modifyRole(RoleDO roleDO) {
    Preconditions.checkArgument(roleDO !=null, "roleDO不能为空");
    Preconditions.checkArgument(roleDO.getId()>0 , "roleDO.id应>0");
    
    int r = this.roleDAO.update(roleDO) ;
    if(r <=0){
      logger.error("roleDO:"+roleDO+",更新失败");
    }
    return Result.buildSuccess(r > 0) ;
  }

   
  @Override
  public Result<RoleDO> queryRoleById(Long roleId) {
    Preconditions.checkArgument( roleId!=null && roleId > 0, "roleId:"+roleId+",应>0");
    
    RoleDO  r = this.roleDAO.load(roleId);
    if(r == null )
      return Result.buildError("roleId:"+roleId+",没有查到角色信息") ;
    return Result.buildSuccess(r);
  }

   
  @Override
  public Result<List<RoleDO>> queryRoles(RoleQuery roleQuery) {
    Preconditions.checkNotNull(roleQuery, "roleQuery不能为空") ; 
    
    int count = this.roleDAO.queryCount(roleQuery) ;
    List<RoleDO> list = null ;
    if(count > 0){
      list =  this.roleDAO.query(roleQuery) ;
      return Result.buildSuccess(count, list);
    }
    return Result.buildError("没有查到角色列表,roleQuery:"+roleQuery);
  }

  
  @Override
  public Result<List<RoleDO>> queryChildRole(Long userId) {
    logger.error("queryChildRole userId=:"+userId); 
    
    if(userId == null ||  userId <=0){
      // return Result.buildError(BaseResultCode.INVALID_PARAMETER) ; 
      //  查出所有角色
      return queryRoles(new RoleQuery()) ;
    }
     
    RoleDO role = this.roleDAO.queryRoleByUserId(userId) ;
    if(role == null ){
      //   return Result.buildError("userId:"+userId+",没有查到此用户的角色") ;
      return queryRoles(new RoleQuery()) ;
    }
      
    List<RoleDO> list = this.roleDAO.queryChildRole(role.getId()) ;
    
    return Result.buildSuccess(list.size(), list);
  }
 
  @Override
  public RoleDO queryRoleByUserId(Long userId) {
    logger.error("queryRoleByUserId userId=:"+userId); 
    Preconditions.checkArgument( userId!=null && userId > 0, "userId:"+userId+",应>0");
    
    return this.roleDAO.queryRoleByUserId(userId); 
    
  }
  
}
